package Model;


import java.awt.image.ColorModel;
import java.awt.image.ImageFilter;

public class ErosionImageFilter extends ImageFilter {

    private int width, height, pixels[];
    ColorModel colorModel = ColorModel.getRGBdefault();

    public void setDimensions(int w, int h) {
        width=w;
        height=h;
        pixels=new int[width * height];
        consumer.setDimensions(w, h);
    }

    public void setColorModel(ColorModel model) {
        consumer.setColorModel(colorModel);
    }
    public void setHints (int hintFlags) {
        consumer.setHints(TOPDOWNLEFTRIGHT | COMPLETESCANLINES | SINGLEPASS | (hintFlags & SINGLEFRAME));
    }

    private void savePixels (int x, int y, int w, int h, ColorModel cm, Object inputPixels, int offset, int scanSize) {
        int sourceOffset = offset;
        int destinationOffset = y * width + x;
        boolean isByteArray = (inputPixels instanceof byte[]);
        for (int j = 0; j < height; j++) {
            for (int i = 0; i < width; i++)
                if (isByteArray)
                    pixels[destinationOffset++] = cm.getRGB(((byte[])inputPixels)[sourceOffset++] & 0xff);
                else
                    pixels[destinationOffset++] = cm.getRGB(((int[])inputPixels)[sourceOffset++]);
            sourceOffset += (scanSize - w);
            destinationOffset += (width - w);
        }
    }

    public void setPixels (int x, int y, int w, int h, ColorModel cm, byte pixelsIdx[], int offset, int scansize) {
        int sourceOffset = offset;
        int destinationOffset = y * width + x;
        for (int j = 0; j < h; j++) {
            for (int i = 0; i < w; i++)
               pixels[destinationOffset++] = cm.getRGB(pixelsIdx[sourceOffset++]);
            sourceOffset += (scansize - w);
            destinationOffset += (width - w);
        }
    }

    public void setPixels (int x, int y, int w, int h, ColorModel cm, int pixelsIdx[], int offset, int scansize) {
        int sourceOffset = offset;
        int destinationOffset = y * width + x;
        for (int j = 0; j < h; j++) {
            for (int i = 0; i < w; i++)
                pixels[destinationOffset++] = cm.getRGB(pixelsIdx[sourceOffset++]);
            sourceOffset += (scansize - w);
            destinationOffset += (width - w);
        }
    }

    public void imageComplete(int status) {
        if ((status != IMAGEABORTED) && (status != IMAGEERROR)) {
            int pos = 0;
            int r = 3;
            int newPixels[] = new int [width * height];
            for (int x = 0; x != width; ++x) {
                for (int y = 0; y != height; ++y) {
                    int min = pixels[pos];
                    min = ((min & 0xff) + ((min & 0xff00) >> 8 )+ ((min & 0xff0000) >> 16)) / 3;
                    int posMin = min;
                    for (int i = Math.max(0, x - r); i <= Math.min(width - 1, x + r); ++i) {
                        for (int j = Math.max(0, y - r); j <= Math.min(height - 1, y + r); ++j) {
                            if (i != x || j != y) {
                                int temp = pixels[j * width + i];
                                temp = ((temp & 0xff) + ((temp & 0xff00) >> 8 )+ ((temp & 0xff0000) >> 16)) / 3;
                                if (temp < min) {
                                    min = temp;
                                    posMin = j * width + i;
                                }
                            }
                        }
                    }
                    newPixels[pos] = pixels[posMin];
                }
            }
           consumer.setPixels(0, 0, width, height, colorModel, newPixels, 0, width);
        }
        consumer.imageComplete (status);
    }
}
